--File ： ChapterView.lua
--Author : zee
--Date   : 2015/4/8
--Description : 


-- difine
BType = {}
BType.one = 1
BType.two_up_down = 2
BType.two_front_back = 3
BType.three = 4



GameHeart = class("GameHeart",function()
    -- 继承
    local ret = cc.Layer:create() 

    ret.myBType = 0
    ret.eneBType = 0
    ret.dic_myGroup = {}
    ret.dic_eneGroup = {}

    --- 操作过程临时变量
    ret.ary_mySelected = {}

    return ret
end)

GameHeart.__index = GameHeart
GameHeart.__instance = nil 



function GameHeart:getInstance()
    if GameHeart.__instance==nil then 
        GameHeart.__instance = GameHeart:new()
        if CURRENT_SCENE then
            CURRENT_SCENE:addChild( GameHeart.__instance )
        end
    end
    return GameHeart.__instance 
end








----------------------------------------------------------
--      启动
----------------------------------------------------------
function GameHeart:start()
    -- 开始计数
    self:startTimeFrame()
    -- 创建双方军队
    self:createGroups()

    -- 创建好以后，军队出场
    self:allGroupEnterToStage() 
    -- 出场后，先做调试动作。
    self:testGroup()

end


-- 启动逐帧函数
function GameHeart:startTimeFrame()
    -- 开始计时。
    local function frameEvent(dt) self:onFrame(dt) end
    BattleLayer.__instance:scheduleUpdateWithPriorityLua( frameEvent, 0 )
end











----------------------------------------------------------
--                  创建军队相关
----------------------------------------------------------
function GameHeart:createGroups(info_my, info_ene)
    -- 给定设置值
    self.myBType = BType.two_up_down  -- one. two_front_back, two_up_down
    self.eneBType = BType.two_up_down
    info_my = {
            mid = {hero=1001, soType=So_dao },
            up = {hero=1001, soType=So_dao },
            down = {hero=1001, soType=So_dao },
            front = {hero=1001, soType=So_dao },
            back = {hero=1001, soType=So_dao },
    }
    info_ene = {
            mid = {hero=1001, soType=So_dao },
            up = {hero=1001, soType=So_qiang },
            down = {hero=1001, soType=So_dao },
            front = {hero=1001, soType=So_dao },
            back = {hero=1001, soType=So_dao },
    }
    --- 根据阵型参数表，取分队。
    self.dic_myGroup = {}
    self.dic_eneGroup = {}
    local myGroups = self:getGroupInfoByType( self.myBType )
    local eneGroups = self:getGroupInfoByType( self.eneBType )

    for groupKey,info in pairs(myGroups) do 
        info.unit = 1
        info.soType = info_my[ groupKey ][ soType ]
        info.hero = info_my[ groupKey ][ hero ]
        info.groupKey = groupKey
        self.dic_myGroup[ groupKey ] = self:addOneGroupByInfo( info )
    end
    for groupKey,info in pairs(eneGroups) do 
        info.unit = -1
        info.soType = info_ene[ groupKey ][ soType ]
        info.hero = info_ene[ groupKey ][ hero ]
        info.groupKey = groupKey
        self.dic_eneGroup[ groupKey ] = self:addOneGroupByInfo( info )
    end
end


-- 根据不同阵型，返回预设的部队类型。
function GameHeart:getGroupInfoByType( gType )
    cclook(" gtype: %s", gType)
    local typeInfo = {
        --- 单列
        [1] = {
            mid = { bigType="big", x_my=18, x_ene=34, line=20, num=140 },
        },
        --- 上下分列
        [2] = {
            up = { bigType="mid", x_my=16, x_ene=36, line=32, num=140 },
            down = { bigType="mid", x_my=16, x_ene=36, line=8, num=140 }
        },
        --- 前后阵型
        [3] = {
            front = { bigType="mid", x_my=8, x_ene=35, line=20, num=140 },
            back = { bigType="mid", x_my=18, x_ene=45, line=20, num=140 }
        },
        --- 三列针
        [4] = {
            up = { bigType="small", x_my=16, x_ene=36, line=32, num=140 },
            mid = { bigType="small", x_my=10, x_ene=44, line=20, num=140 },
            down = { bigType="small", x_my=16, x_ene=36, line=8, num=140 }
        },        
    }
    cclook("  get type info : \n%s", typeInfo[gType] )
    return typeInfo[ gType ]
end



-- 添加一个军团
function GameHeart:addOneGroupByInfo( info )
    local one = BGroup:create()
    one.dic_initInfo = info
    one.groupKey = info.groupKey
    one:setUnit( info.unit )
    one:setSoType( info.soType )
    one:setBigType( info.bigType )
    local mapX = info.x_my
    if info.unit==-1 then 
        mapX=info.x_ene 
    end
    one:setCenter( mapX+(-info.unit)*10, info.line  )
    one:setNumber( info.num )
    one:setHeroId( info.hero )
    one:createSoliderOnMap( BattleLayer.__instance.ground )
    -- 让部队显示按钮
    local btn = GBtn:create()
    one:showBtnOnMap( btn, BattleLayer.__instance.ground, info.unit==1 )
    btn:setGroupAfterShow( one )
    -- 挂一个临时事件，让部队翻转的。
    local function groupClickEvt()
        cclook("操你妈逼")
        -- one:Group_TurnBack()
        one:moveToXYWithType( 84, one.line, "rush" )
    end
    btn.func = groupClickEvt
    
    return one 
end






----------------------------------------------------------
--                  军团出场表演。
----------------------------------------------------------
function GameHeart:allGroupEnterToStage( oneGroup )
    for _,group in pairs(self.dic_myGroup) do 
        GameHeart:OneGroupEnterToStage(group)
    end
    for _,group in pairs(self.dic_eneGroup) do 
        GameHeart:OneGroupEnterToStage(group)
    end
end



function GameHeart:OneGroupEnterToStage( oneGroup )
    -- 部队奔跑着从场景外出来。
    oneGroup:moveToXYWithType( oneGroup.mapX+oneGroup.unit*10, oneGroup.line+3, "rush" )
end











----------------------------------------------------------
--                  行为调试
----------------------------------------------------------
function GameHeart:testGroup()
    -- 延迟10秒，进行反转奔走
    local function func()
        for _,group in pairs(self.dic_myGroup) do 
            -- group:moveToXYWithType( 2, group.line, "rush" )
        end
    end
    cc.Director:sharedDirector():getScheduler():scheduleScriptFunc(
        func, 10, false)
end





















----------------------------------------------------------
--              给敌我的每个group寻找盯防和进攻目标。
----------------------------------------------------------
function GameHeart:findEachGroupAim()
    if self.myBType==self.eneBType then 
        -- 如果阵型相同，就找自己的对应对手。
        for gName,group in pairs(self.dic_myGroup) do 
            local aimGroup = self.dic_eneGroup[gName]
            -- 活着的对手，找对应的。
            if(aimGroup.alive==true) then
                group.attAimGroup = aimGroup
                group.attType = ATT_JIN_GONG

            -- 对应的军团已死，就找任何一个活着的。
            elseif (aimGroup.alive==false) then
                for _,eneGroup in pairs(self.dic_eneGroup) do 
                    if eneGroup.alive==true then 
                        aimGroup = eneGroup
                        break;
                    end
                end
                group.attType = ATT_JIN_GONG
            end
        end

        -- 如果阵型相同，就找自己的对应对手。
        for gName,group in pairs(self.dic_eneGroup) do 
            local aimGroup = self.dic_myGroup[gName]
            -- 活着的对手，找对应的。
            if(aimGroup.alive==true) then
                group.attAimGroup = aimGroup
                group.attType = ATT_JIN_GONG

            -- 对应的军团已死，就找任何一个活着的。
            elseif (aimGroup.alive==false) then 
                for _,eneGroup in pairs(self.dic_myGroup) do 
                    if eneGroup.alive==true then 
                        aimGroup = eneGroup
                        break;
                    end
                end
                group.attType = ATT_JIN_GONG
            end
        end
    end
end
























----------------------------------------------------------
--              逐帧分发。
----------------------------------------------------------
function GameHeart:onFrame(dt)

    -- 根据阵型类型检查每个 group 的目标。
    self:findEachGroupAim()

end











----------------------------------------------------------
--      操作响应
----------------------------------------------------------








